package com.lily.aidouzixun.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.lily.aidouzixun.R;
import com.lily.aidouzixun.ui.MulitPointTouchListener;
import com.lily.aidouzixun.ui.TitleBar;
import com.lily.aidouzixun.util.DataConverter;
import com.lily.aidouzixun.util.HardWare;
import com.lily.aidouzixun.util.Validator;
import com.lily.aidouzixun.util.MyConstant.*;
/**
 * 用于显示大图的Activity
 */
public class ShowImageActivity  extends Activity {
    private static final String TAG = "ShowImageActivity";

    private TitleBar mTitleBar;
    private ImageView ivShowImage;  //显示图片
    private Bitmap mBitmap;
    private String imagePath;
    private String imageUrl;

    private Activity mActivity;
    private static final String TITLE = "查看图片";
    private int imageFrom = 0;  //图片来源

    /**
     * 图片来源类型
     */
    public interface ImageFrom {
        /**
         * 通过Intent传递Bitmap对象展示
         */
        int  BITMAP = 1;
        /**
         * 通过url展示图片
         */
        int URL = 2;
        /**
         * 图片文件路径展示图片
         */
        int FILE_PATH = 3;
        /**
         * 文件URI展示图片
         */
        int  FILE_URI = 4;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.showimage);
        init();
        findViews();
        setLisenters();
    }

    private void init() {
        mActivity = ShowImageActivity.this;
        Intent intent = getIntent();
        if (intent != null) {
            imageFrom = intent.getIntExtra(IntentKey.IMAGE_FROM, 0);
            switch (imageFrom) {
                case ImageFrom.BITMAP:
                    mBitmap = intent.getParcelableExtra(IntentKey.BITMAP);
                    break;
                case ImageFrom.URL:
                    imageUrl = intent.getStringExtra(IntentKey.IMAGE_URL);
                    break;
                case ImageFrom.FILE_PATH:
                    imagePath = intent.getStringExtra(IntentKey.IMAGE_PATH);
                    mBitmap = DataConverter.getBitmapFormFilePath(imagePath, HardWare.getScreenHeight(getApplicationContext()), HardWare.getScreenWidth(getApplicationContext()));
                    break;
                case ImageFrom.FILE_URI:

                    break;
            }

        }
    }

    private void findViews() {
        mTitleBar = (TitleBar) this.findViewById(R.id.mytitlebar);
        mTitleBar.setTitle(TITLE);
        mTitleBar.setLeftOperation("返回");
        mTitleBar.setBgColor(Color.GRAY);
        mTitleBar.setCurActivity(mActivity);
        ivShowImage = (ImageView) this.findViewById(R.id.iv_showimage);

        switch (imageFrom) {
            case ImageFrom.BITMAP:
                ivShowImage.setImageBitmap(mBitmap);
                break;
            case ImageFrom.URL:
                if (Validator.isEffective(imageUrl)) {
                    Glide.with(mActivity).load(imageUrl).into(ivShowImage);
                }
                break;
            case ImageFrom.FILE_PATH:
                if (mBitmap != null) {
                    ivShowImage.setImageBitmap(mBitmap);
                }
                break;
            case ImageFrom.FILE_URI:

                break;
        }

    }

    private void setLisenters() {
        //图片
        ivShowImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //ShowImageActivity.this.finish();
            }
        });
        //设置触摸事件
        ivShowImage.setOnTouchListener(new MulitPointTouchListener());
    }
}
